package com.seawaterbt.springbootshiro.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.seawaterbt.springbootshiro.entity.User;
import com.seawaterbt.springbootshiro.enums.ErrorCode;
import com.seawaterbt.springbootshiro.result.ResultData;
import com.seawaterbt.springbootshiro.service.UserService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Api("用户管理")
@Controller
@RequestMapping("/user")
public class UserControler {

    @Autowired
    private UserService userService;

    @GetMapping("/manager")
    //@RequiresRoles(value = {"1","2","3"},logical = Logical.OR)
    //@RequiresPermissions({"system:manager","user:manager","user:list"})
    public String manager(){return "user/list";}

    @GetMapping("/toAdd")
    //@RequiresRoles(value = {"1","3"},logical = Logical.OR)
    //@RequiresPermissions({"system:manager","user:manager","user:add"})
    public String add(){
        return "user/add";
    }

    @PostMapping("/add")
    @ResponseBody
    //@RequiresRoles(value = {"1","3"},logical = Logical.OR)
    //@RequiresPermissions({"system:manager","user:manager","user:add"})
    public ResultData add(@RequestBody User user){
        if(Objects.isNull(user)) {
            return ResultData.failure(ErrorCode.PARAMETER_INCOMPLETE);
        }
        user.setStatus(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        boolean insert = userService.insert(user);
        return insert ? ResultData.ok() : ResultData.failure(ErrorCode.SERVER_SYSTEM_ERROR);
    }

    @DeleteMapping("delete/{id}")
    @ResponseBody
    //@RequiresRoles(value = {"1","3"},logical = Logical.OR)
    //@RequiresPermissions({"system:manager","user:manager","user:delete"})
    public ResultData delete(Integer id){
        User user = userService.selectOne(new EntityWrapper<User>().eq("uid", id));
        user.setStatus(3);
        boolean delete = userService.updateById(user);
        return delete ? ResultData.ok() : ResultData.failure(ErrorCode.SERVER_SYSTEM_ERROR);
    }

    @GetMapping("/toUpdate/{id}")
    //@RequiresRoles(value = {"1","3"},logical = Logical.OR)
    //@RequiresPermissions({"system:manager","user:manager","user:update"})
    public String update(Integer id, Model model){
        model.addAttribute("id",id);
        return "user/add";
    }

    @PostMapping("/update")
    @ResponseBody
    //@RequiresRoles(value = {"1","3"},logical = Logical.OR)
    //@RequiresPermissions({"system:manager","user:manager","user:update"})
    public ResultData update(User user){
        if(Objects.isNull(user) || Objects.isNull(user.getUid())) {
            return ResultData.failure(ErrorCode.PARAMETER_INCOMPLETE);
        }
        boolean update = userService.updateById(user);
        return update ? ResultData.ok() : ResultData.failure(ErrorCode.SERVER_SYSTEM_ERROR);
    }

    @GetMapping("/list")
    @ResponseBody
    //@RequiresRoles(value = {"1","2","3"},logical = Logical.OR)
    //@RequiresPermissions({"system:manager","user:manager","user:list"})
    public ResultData list(User user, Page<User> page){
        Page<User> userPage = userService.selectPage(page, new EntityWrapper<>(user));
        return ResultData.ok(0,"",userPage.getRecords(),Long.valueOf(userPage.getTotal()));
    }
}
